package com.cino.service.impl;

import com.cino.domain.LoginInfo;
import com.cino.exception.ContentEmptyException;
import com.cino.exception.PasswordCertificateException;
import com.cino.mapper.LoginMapper;
import com.cino.service.LoginService;
import com.cino.util.AjaxResult;
import com.cino.util.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;


    @Override
    public AjaxResult login(LoginInfo loginInfo) throws ContentEmptyException, PasswordCertificateException {
        LoginInfo user = loginMapper.login(loginInfo);

        if(loginInfo.getUsername()==null || "".equals(loginInfo.getUsername().trim())){
            throw new ContentEmptyException("用户名不能为空!!");
        }

        if(loginInfo.getPassword()==null || "".equals(loginInfo.getPassword().trim())){
            throw new ContentEmptyException("密码不能为空!!");
        }

        if(!user.getPassword().equals(loginInfo.getPassword())){
            throw  new PasswordCertificateException("密码不正确");
        }

        //来到这里说明登录成功
        String token = UUID.randomUUID().toString();
        UserSession.setUserSession(token,loginInfo);

        return  AjaxResult.success(token);
    }
}
